package com.yDev.rockclimber;

import java.io.*;

public class MyFiles {

    File file;

    public static String readFileLine(String fileName, int line) {
        // System.out.print(file.getAbsoluteFile() + "\n");
        try {
            FileInputStream fstream = new FileInputStream(fileName);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            if (line == 0) {
                return br.readLine();
            }
            for (int i = 0; i < line; i++) {
                br.readLine();
            }
            return br.readLine();
        } catch (Exception e) {//Catch exception if any
           // System.err.println("Error: " + e.getMessage());
        }
        return "";
    }

    public static void createFile(String fileName, String content) throws IOException {
        File file = new File(fileName);
        //System.out.print(file.getAbsoluteFile() + "\n");
        boolean exist = file.createNewFile();
        if (!exist) {
            //System.out.println("File already exists.");
        } else {
            FileWriter fstream = new FileWriter(fileName);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(content);
            out.close();
            //System.out.println("File created successfully.");
        }
    }

    public static void createFileOrAppend(String fileName, String content) throws IOException {
        File file = new File(fileName);
        //System.out.print(file.getAbsoluteFile() + "\n");
        file.createNewFile();

        FileWriter fstream = new FileWriter(fileName, true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(content);
        out.close();
       // System.out.println("File created successfully.");
    }
}
